/**
 * Created by MyPC on 2017/7/16.
 */
var express = require('express');
var router = express.Router();
let logger = require('log4js').getLogger('tagRoutes');
let TagModel = require('../../schemas/tag');
let returnJson = require('../../common/resultJson');

router.get('/all', function (req, res, next) {
  let query = TagModel.find({

  }).exec(function (err, result) {
    if(err){
      logger.error('查询tag表报错:', err);
      returnJson(res, '系统繁忙,请稍后再试');
    }else{
      returnJson(res, 1, '???????????', result);
    }
  });
});

router.post('/', function (req, res, next) {
  let promise = new Promise(function (resolve, reject) {
    var tag = new TagModel(req.body);
    let query = tag.save(function (err, result) {
      if(err){
        logger.error('查询tag表报错:', err);
        reject(err);
      }else{
        resolve(result);
      }
    });
  })
  promise.then(function (result) {
    console.log(result);
    returnJson(res, 1, '???????????', result);
  }).catch(function (err) {
    returnJson(res, '系统繁忙,请稍后再试');
  })
});

module.exports = router;
